import json

from flask import render_template,request,jsonify
from . import projects
import time

import os
from frameworks.cores.Config import *
from frameworks.utils.JsonResult2 import *
from frameworks.cores.LoadClass import *

basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
config = Config(basepath)

apimap = config.getApi()
env = apimap["base"]
print(env)
from flask_session import Session

jsonResult = JsonResult2()

loader = LoadClass()

@projects.route('/project/list')
def project_list():
    return render_template('projects/project_list.html')

@projects.route(env+"/project/update",methods=['POST'])
def update():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.projects.ProjectController", "ProjectController", {
        "func": "update",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "更新成功")

@projects.route(env+"/project/delete",methods=['GET'])
def delete():
    id = request.args.get("id",0)
    print(id)
    rs = loader.load("controller.projects.ProjectController", "ProjectController", {
        "func": "delete",
        "data": {"id":id}
    })
    return jsonResult.setOk([], "删除成功")

@projects.route(env+"/project/add",methods=['POST'])
def add():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.projects.ProjectController", "ProjectController", {
        "func": "add",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "添加成功")

@projects.route(env+"/project/form",methods=['GET', 'POST'])
def form():
    model = request.args.get("model", "school")
    id = request.args.get("id", "0")
    obj = loader.load("controller.projects.ProjectController","ProjectController",{
        "func":"getRowData",
        "fields":["id","project_name"],
        "id": id
    })
    fields = loader.load("controller.projects.ProjectController","ProjectController",{
        "func":"getMap",
        "fields":["id","project_name"]
    })
    return render_template(
        'projects/project_form.html',
        obj=obj[0],
        fields=fields
    )

@projects.route(env+"/project/deploy_form",methods=['GET', 'POST'])
def deploy_form():
    id = request.args.get("id", "0")
    obj = loader.load("controller.projects.ProjectController","ProjectController",{
        "func":"getRowData",
        "fields":["id","project_name"],
        "id": id
    })
    #获取git仓库信息
    gitData = loader.load("controller.respostory_manage.GitReposController", "GitReposController", {
        "func": "getAllData",
        "fields": ["id", "repos_name","uri","username","password"],
    })
    print(gitData)
    # 获取主机信息
    hostData = loader.load("controller.servers.ServerController", "ServerController", {
        "func": "getAllData",
        "fields": ["id", "server_name","ip"],
    })
    print(hostData)
    return render_template(
        'projects/project_deploy_form.html',
        obj=obj[0],
        gitData=gitData,
        hostData=hostData
    )

@projects.route(env+"/project/deploy_exec",methods=['POST'])
def deploy_exec():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.projects.ProjectQueueController", "ProjectQueueController", {
        "func": "addDeployQueue",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "添加成功")

@projects.route(env+"/project/formadd",methods=['GET', 'POST'])
def formadd():
    model = request.args.get("model", "school")
    fields = loader.load("controller.projects.ProjectController","ProjectController",{
        "func":"getMap",
        "fields":["id","project_name"]
    })
    return render_template(
        'projects/project_formadd.html',
        fields=fields
    )

@projects.route(env+"/project/cols",methods=['GET','POST'])
def cols():
    width = request.args.get("width", "200")
    rs = loader.load("controller.projects.ProjectController", "ProjectController", {
        "func":"getColumns",
        "fields":["id","project_name","ctime","mtime"],
        "width": width
    })
    return jsonResult.setOk(rs,"获取成功")

@projects.route(env+"/project/page",methods=['GET'])
def user():
    num = request.args.get("limit",10)
    page = request.args.get("page",1)
    data = loader.load("controller.projects.ProjectController", "ProjectController", {
        "func": "getData",
        "fields": ["id","project_name","ctime","mtime"],
        "page": page,
        "num": num,
        "orderfield": "id",
        "asc": "asc"
    })
    return json.dumps(data)